﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;

namespace WCFService
{
    // NOTE: If you change the class name "ChatService" here, you must also update the reference to "ChatService" in App.config.

    [ServiceBehavior(InstanceContextMode = InstanceContextMode.Single, ConcurrencyMode = ConcurrencyMode.Multiple, UseSynchronizationContext = false)]
    public class ChatService : IChatService
    {

        static Dictionary<Client, IChatCallback> clients = new Dictionary<Client, IChatCallback>();
        private static List<Client> clientList = null;
        public ChatService()
        {
            clientList = getClients();
        }

        List<Client> getClients()
        {
            List<Client> listClient = new List<Client>();
            for (int i = 1; i < 10; i++)
            {
                Client client = new Client();
                client.Name = "Khoi" + i.ToString();
                client.Status = 0;
                listClient.Add(client);
            }

            return listClient;
        }

        public IChatCallback CurrentCallback
        {
            get
            {
                return OperationContext.Current.GetCallbackChannel<IChatCallback>();
            }
        }

        object objSyn = new object();



        #region IChatService Members

        public bool Connect(Client client)
        {
            lock (objSyn)
            {
                foreach (Client cl in clientList)
                {
                    if (cl.Name == client.Name)
                    {
                        clients.Add(client, CurrentCallback);
                        for (int i = 0; i < clientList.Count; i++)
                        {
                            if (clientList[i].Name == client.Name)
                            {
                                clientList[i].Status = 1;
                            }
                        }

                        foreach (Client key in clients.Keys)
                        {
                            IChatCallback callback = clients[key];
                            try
                            {
                                //callback.RefreshClients(clientList);
                                callback.UserJoint(client);
                                callback.HasNotify("Da online");
                            }
                            catch (Exception ex)
                            {
                                return false;
                            }
                        }

                        return true;
                    }
                }
            }
                
            return false;
        }

        public void Say(Message msg)
        {
            foreach (IChatCallback callback in clients.Values)
            {
                callback.Receive(msg);
            }
        }

        public void Whisper(Message msg, Client receiver)
        {
            foreach (Client key in clients.Keys)
            {
                if (key.Name == receiver.Name)
                {
                    IChatCallback callback = clients[key];
                    try
                    {
                        callback.Receive(msg);
                    }
                    catch (Exception exp)
                    {
                    }
                }
            }
        }

        public void Disconnect(Client client)
        {
            //clients.Remove(client);
            bool remove = clients.Remove(client);
            foreach (Client key in clients.Keys)
            {
                if (key.Equals(client))
                {
                    clients.Remove(key);
                }
            }
            
            for (int i = 0; i < clientList.Count; i++)
            {
                if (clientList[i].Name == client.Name)
                {
                    clientList[i].Status = 0;
                    break;
                }
            }

            foreach (Client key in clients.Keys)
            {
                IChatCallback callback = clients[key];
                try
                {
                    callback.RefreshClients(clientList);
                }
                catch (Exception ex)
                {
                    return;
                }
            }
        }

        public static void Notify(string message)
        {
            foreach (IChatCallback callback in clients.Values)
            {
                callback.HasNotify(message);
            }
        }

        #endregion
    }
}
